/* $RCSFile: HtmlModuleDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/HTMLModule/HtmlModuleDB.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:55  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Collections;
using System.Globalization;

using AddonNice.Configuration;
using AddonNice.Diagnostics;


namespace AddonNice.Settings.PageModules.HtmlModule
{
	/// <summary>
	/// Class that encapsulates all data logic necessary to add/query/delete
	/// HTML/text within the Portal database.
	/// </summary>
    public class HtmlModuleDB : DBDataStore
    {
		/// <summary>
		/// The GetHtmlText method returns a SqlDataReader containing details
		/// about a specific item from the HtmlText database table.
		/// </summary>
        public SqlDataReader GetHtmlTextGlobalized(int moduleID) 
        {
            SqlCommand myCommand            =   new SqlCommand("dbo.adn_GetHtmlTextGlobalized", myConnection);
            myCommand.CommandType           =   CommandType.StoredProcedure;

			AddGlobalizeParameters(myCommand);

            AddModuleIDParameters(myCommand,moduleID);

			SqlDataReader result			=   null;
			try
			{
				myConnection.Open();
				result						=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HTMLModuleDB GetHtmlTextGlobalized moduleID: {0}, ex: {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}
        /// <summary>
        /// Old version without LCID management
        /// </summary>
        public SqlDataReader GetHtmlText(int moduleID) 
        {
            SqlCommand myCommand            =   new SqlCommand("dbo.adn_GetHtmlText", myConnection);
            myCommand.CommandType           =   CommandType.StoredProcedure;

            AddModuleIDParameters(myCommand,moduleID);

			SqlDataReader result			=   null;
			try
			{
				myConnection.Open();
				result						=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HTMLModuleDB GetHtmlText moduleID: {0}, ex: {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}

		/// <summary>
		/// The UpdateHtmlText method updates a specified item within
		/// the HtmlText database table.
		/// </summary>
        public void UpdateHtmlTextGlobalized(int moduleID, String desktopHtml) 
        {
            SqlCommand myCommand                =   new SqlCommand("dbo.adn_UpdateHtmlTextGlobalized", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

			AddGlobalizeParameters(myCommand);
            AddModuleIDParameters(myCommand,moduleID);

            SqlParameter parameterDesktopHtml   =   new SqlParameter("@DesktopHtml", SqlDbType.NVarChar,desktopHtml.Length);
            parameterDesktopHtml.Value          =   desktopHtml;
            myCommand.Parameters.Add(parameterDesktopHtml);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlModuleDB UpdateHtmlTextGlobalized moduleID: {0}, ex : {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

        /// <summary>
        /// Old version without LCID
        /// </summary>
        public void UpdateHtmlText(int moduleID, String desktopHtml) 
        {
            SqlCommand myCommand                =   new SqlCommand("dbo.adn_UpdateHtmlText", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

            AddModuleIDParameters(myCommand,moduleID);

            SqlParameter parameterDesktopHtml   =   new SqlParameter("@DesktopHtml", SqlDbType.NVarChar,desktopHtml.Length);
            parameterDesktopHtml.Value          =   desktopHtml;
            myCommand.Parameters.Add(parameterDesktopHtml);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlModuleDB UpdateHtmlText moduleID: {0}, ex : {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}
    }
}
